package com.hmkj.quote.controller;

import com.hmkj.core.po.user.User;
import com.hmkj.core.service.cache.RedisService;
import com.hmkj.core.service.order.OrderService;
import com.hmkj.core.service.risk.RiskParamService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 行情请求相关控制器
 *
 * @author Vector
 * @create 2017-11-13 11:57
 */
@Api(description = "行情请求相关")
@Slf4j
@RestController
@RequestMapping(GenericController.BASE_URL)
public class QuotationController extends GenericController<Integer, User> {
    @Resource
    private RedisService redisService;
    @Resource
    private RiskParamService riskParamService;
    @Resource
    private OrderService orderService;

//
//    @ApiOperation(value = "获取分时数据", notes = "获取分时数据")
//    @RequestMapping(value = "getTrendDataOut", method = RequestMethod.POST)
//    public ResultEntity<List<TrendData>> getTrendDataOut(@ApiParam("品种ID") @RequestParam Integer commoId,
//                                                      @ApiParam("数据条数") @RequestParam String number) throws Exception {
//        log.info("获取分时数据");
//        Commo commo = redisService.get(CacheID.COMMO_ID_PREFIX + commoId, Commo.class);
//        if (commo == null) throw new BussinessException("请求品种不存在");
//        String exchType = riskParamService.getParamValue(RiskNID.EXCH_TYPE, commoId);
//        String contract = riskParamService.getParamValue(RiskNID.CONTRACT, commoId);
//        return ok(QuotationCall.getInstance().getTrendDataOut(exchType, commo.getCode(), contract, number));
//    }
//
//
//    @ApiOperation(value = "获取K线数据", notes = "获取K线数据")
//    @RequestMapping(value = "getKlineData", method = RequestMethod.POST)
//    public ResultEntity<List<KlineData>> getKlineData(@ApiParam("品种ID") @RequestParam Integer commoId,
//                                                      @ApiParam("K线类型：M1:1分钟K线 M3:3分钟K线 M5:5分钟K线 M15:15分钟K线 M30:30分钟K线 M60:60分钟K线 D1:日K") @RequestParam String type,
//                                                      @ApiParam("数据条数，查询日K是必填") @RequestParam(required = false) String number) throws Exception {
//        log.info("获取K线数据");
//        Commo commo = redisService.get(CacheID.COMMO_ID_PREFIX + commoId, Commo.class);
//        if (commo == null) throw new BussinessException("请求品种不存在");
//        String exchType = riskParamService.getParamValue(RiskNID.EXCH_TYPE, commoId);
//        String contract = riskParamService.getParamValue(RiskNID.CONTRACT, commoId);
//
//        if (type.equals(KlineData.TYPE.D1.value)) {
//            return ok(QuotationCall.getInstance().getDKlineData(exchType, commo.getCode(), contract, number));
//        } else if (type.equals(KlineData.TYPE.M1.value)) {
//            return ok(buildM1K(QuotationCall.getInstance().getTrendDataOut(exchType, commo.getCode(), contract, "60")));
//        } else {
//            return ok(QuotationCall.getInstance().getKlineData(exchType, commo.getCode(), contract, KlineData.TYPE.getCode(type)));
//        }
//
//    }
//
//    /**
//     * 1分钟K线使用分数数据进行转化
//     *
//     * @param list
//     * @return
//     */
//    private List<KlineData> buildM1K(List<TrendData> list) {
//        if (list != null) {
//            List<KlineData> klist = new ArrayList<>();
//            list.forEach(item -> {
//                KlineData data = new KlineData();
//                data.setOp(item.getOp());
//                data.setCp(item.getCp());
//                data.setLp(item.getLp());
//                data.setHp(item.getHp());
//                data.setDt(item.getDt());
//                data.setMt(item.getMt());
//                data.setD(item.getD());
//                klist.add(data);
//            });
//            return klist;
//        }
//        return null;
//    }
//
//
//    @ApiOperation(value = "获取期货风控参数", notes = "获取期货风控参数")
//    @RequestMapping(value = "getRiskData", method = RequestMethod.POST)
//    public ResultEntity getRiskData(@ApiParam("品种ID") @RequestParam Integer commoId) throws Exception {
//        log.info("获取期货风控参数");
//        return ok(m -> {
//            riskParamService.select(new RiskParam(t -> {
//                t.setCommoId(commoId);
//                t.setDelFlag(GenericPo.DELFLAG.NORMAL.code);
//            })).stream().forEach(riskParam -> {
//                m.put(riskParam.getNid(), riskParam.getValue());
//            });
//            Commo commo = redisService.get(CacheID.COMMO_ID_PREFIX + commoId, Commo.class);
//            m.put("name", commo.getName());
//            m.put("code", commo.getCode());
//            m.put("contract", riskParamService.getContractNo(commoId));
//            m.put("is_trade", riskParamService.isTradeTime(commoId));
//            if ("HSI".equals(commo.getCode()) || "MHI".equals(commo.getCode())) {
//                m.put("currency_type", "CNY");
//            } else {
//                m.put("currency_type", "USD");
//            }
//        });
//    }
//
//
//    @ApiOperation(value = "获取期货实时行情数据", notes = "获取期货实时行情数据")
//    @RequestMapping(value = "getRealData", method = RequestMethod.POST)
//    public ResultEntity<RealTimeData> getRealData(@ApiParam("品种ID") @RequestParam Integer commoId) throws Exception {
//        log.info("获取期货实时行情数据");
//        return ok(redisService.get(CacheID.QUOTE_REAL_PREFIX + commoId, RealTimeData.class));
//    }
//
//
//    @ApiOperation(value = "期货单个品种简要行情", notes = "期货单个品种简要行情")
//    @RequestMapping(value = "simpleQuotation", method = RequestMethod.POST)
//    public ResultEntity simpleQuotation(@ApiParam("品种ID") @RequestParam Integer commoId) throws Exception {
//        log.info("期货单个品种简要行情");
//        return ok(redisService.mapGet(CacheID.QUOTE_SIMPLEREAL_MAP, commoId.toString(), SimpleRealData.class));
//    }
//
//    @ApiOperation(value = "期货所有品种简要行情", notes = "期货所有品种简要行情")
//    @RequestMapping(value = "allSimpleQuotation", method = RequestMethod.POST)
//    public ResultEntity allSimpleQuotation() throws Exception {
//        log.info("期货所有品种简要行情");
//        return ok(redisService.map(CacheID.QUOTE_SIMPLEREAL_MAP));
//    }
}
